package scm.erp.sale.order.service;

import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import scm.erp.sale.order.domain.Order;
import scm.erp.sale.order.enums.OrderStatus;
import scm.erp.sale.order.mapper.OrderMapper;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class OrderService {
    private final OrderMapper orderMapper;
    public OrderService(OrderMapper mapper) {this.orderMapper = mapper;}

    /**
     * 全部查询
     */
    public List<Order.Entity> list(Order.SearchVo searchVo) {
        List<Order.Entity> entityList = this.orderMapper.select(searchVo);
        log.info("查询数量:{}", entityList.size());
        return entityList;
    }


    /**
     * 查询计划编码
     */
    public String searchOrderCode(Order.searchOrderCodeVo searchVo) {
        return searchVo.getOrderCode();
    }
    /**
     * 新增
     */
    public String insert(Order.Vo vo) {
        log.info("准备新增订单...");
        int ret = orderMapper.insert(vo);
        log.info("新增订单结果:{}", ret);
        return vo.getId();
    }

    /**
     * 更新
     */
    public Boolean update(Order.Vo vo) {
        log.info("准备更新订单...");
        int updateNum = orderMapper.update(vo);
        log.info("更新订单结果:{}", updateNum);
        return updateNum == 1;
    }

    public Order.Vo one(String id) {
        Order.Entity one = this.orderMapper.selectById(id);
        if (one == null) {
            log.warn("订单不存在!");
            return null;
        }
        return one.convert(Order.Vo.class);
    }

    /**
     * 批量删除
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void batchRemove(List<String> ids) {
        int idList = this.orderMapper
                .removeByIds(ids);
        if (idList  > 0) {
            log.info("删除订单结果:{}", idList);
        }
    }


    /**
     * remove
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void remove(String id) {
        int deleteNum = this.orderMapper.remove(id);
        log.info("删除订单结果:{}", deleteNum);
    }

    /**
     *逻辑删除
     * @param id
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void delete(String id) {
        int deleteNum = this.orderMapper.delete(id);  // 仅保留一次
        log.info("逻辑删除订单结果:{}", deleteNum);
    }
    /**
     * 物理批量删除
     *
     * @param ids 品系ID
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void deleteByIds(List<String> ids) {
        int idList = this.orderMapper
                .deleteByIds(ids);
        if (idList  > 0) {
            log.info("删除订单结果:{}", idList);
        }
    }

    /**
     * 导出
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void export(Order.SearchVo vo, HttpServletResponse response) {
        List<Order.Entity> seriesList = this.orderMapper.select(vo);
    }

    /**
     * 批量新增
     */
    @Transactional(rollbackFor = Exception.class)
    public List<String> insertBatch(List<Order.Vo> insertVoList) {
        return insertVoList
                .stream()
                .map(this::insert)
                .collect(Collectors.toList());
    }

    /**
     * 默认
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int updateOrderStatusDefault(List<String> ids) {
        int updateStatus = this.orderMapper
                .updateOrderStatusDefault(ids);
        log.info("更新状态返回结果:{}", updateStatus);
        return updateStatus;
    }
    public int updateOrderStatus(Order.OrderStatusUpdateRequest request) {
        OrderStatus status = request.getOrderStatus();
        int updateStatus = switch (status) {
            case DEFAULT -> orderMapper.updateOrderStatusDefault(request.getIds());
            case SIGN -> orderMapper.updateOrderStatusSign(request.getIds());
            case ENTRANCE -> orderMapper.updateOrderStatusEntrance(request.getIds());
            case TARE -> orderMapper.updateOrderStatusTare(request.getIds());
            case SHIPPING -> orderMapper.updateOrderStatusShipping(request.getIds());
            case ROUGH -> orderMapper.updateOrderStatusRough(request.getIds());  // 修正
            case LEAVE -> orderMapper.updateOrderStatusLeave(request.getIds());  // 修正
            case FINISH -> orderMapper.updateOrderStatusFinish(request.getIds());  // 修正
            case CHECK -> orderMapper.updateOrderStatusCheck(request.getIds());  // 修正
            case APPLY -> orderMapper.updatePlanStatusApply(request.getIds());  // 修正
            default -> {
                log.error("不支持的状态类型：{}", status);
                throw new IllegalArgumentException("状态类型不合法");
            }
        };
        log.info("更新状态返回结果:{}", updateStatus);
        return updateStatus;
    }

}
